package com.my.controller;

import com.my.anno.Log;
import com.my.entity.Dept;
import com.my.entity.Result;
import com.my.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 请求处理类
 */
@RestController   //等价于Controller + ResponseBody
public class DeptController {

    @Autowired
    private DeptService deptService;

    /**
     * 获取部门列表
     * @param dept
     * @return
     */
    @GetMapping("/depts")
    public Result getDepts(Dept dept){
        List<Dept> depts = deptService.getDepts();
        return Result.success(depts);
    }

    @DeleteMapping("/depts") // 等同 RequestMapping(value="/depts", method= RequestMethod.DELETE)

    // @RequestParam必须传递参数 否则报400 或者改requeired 也能省略
    // 可以简写成 Integer id
    @Log
    public Result deleteDept(Integer id){
        System.out.println("deptId+" + id);
        deptService.delete(id);
        return Result.success();
    }

    /**
     * 新增部门
     * @param {"name":"教研部"} json
     */
    @Log
    @PostMapping("/depts") // 请求服务地址 =》 add服务
    public Result addDept(@RequestBody Dept dept){
        System.out.println(dept);
        deptService.addDept(dept);
        return Result.success();
    }

    /**
     * 修改部门
     */
    @Log
    @PutMapping("/depts")
    public Result updateDept(@RequestBody Dept dept){
        System.out.println("修改的部门：" + dept);
        deptService.updateDept(dept);
        return Result.success(dept);
    }

    /**
     * 通过id查询数据
     */
    @GetMapping("/depts/{id}")
    public Result getDeptById(@PathVariable Integer id){
        System.out.println("id = " + id);
        Dept dept = deptService.getDeptById(id);
        return Result.success(dept);
    }
}
